package com.chinaunicom.bigdata.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinaunicom.bigdata.dto.IpOnlineDayDto;
import com.chinaunicom.bigdata.model.AddrDayBean;
import com.chinaunicom.bigdata.model.AddrTotalpvBean;
import com.chinaunicom.bigdata.model.HourOnlineBean;
import com.chinaunicom.bigdata.model.IpAddrBean;
import com.chinaunicom.bigdata.model.IpOnlineBean;
import com.chinaunicom.bigdata.service.IAddrDayService;
import com.chinaunicom.bigdata.service.IAddrTotalpvService;
import com.chinaunicom.bigdata.service.IHourOnlineService;
import com.chinaunicom.bigdata.service.IIpAddrService;
import com.chinaunicom.bigdata.service.IIpOnlineService;

@Controller
@RequestMapping("/crm/bigdata")
public class MainController {
	
	@Resource
	IAddrDayService service;
	@Resource
	IAddrTotalpvService atservice;
	@Resource
	IIpOnlineService iposervice;
	@Resource
	IIpAddrService ipadservice;
	@Resource
	IHourOnlineService hoservice;
	
	
	@RequestMapping("/bdmain")
	public String bdmain(Model model)
	{
		List<AddrDayBean> list = service.query(null);
		model.addAttribute("addrMenu", list);
		List<IpOnlineBean> iplist = iposervice.query(null);
		model.addAttribute("ipMenu", iplist);
		return "/WEB-INF/bigdata/bdmain.jsp";
	}
	
	@ResponseBody
	@RequestMapping("/query")
	public Map<String,Map<String,Object>> query(String address)
	{
		AddrDayBean adbean = new AddrDayBean();
		adbean.setAddress(address);
				
		ArrayList<AddrDayBean> list = (ArrayList<AddrDayBean>) service.query(adbean);
		ArrayList<AddrDayBean> hourlist = (ArrayList<AddrDayBean>) service.queryByHour(adbean);
		ArrayList<AddrTotalpvBean> totallist = (ArrayList<AddrTotalpvBean>) atservice.query(null);
		
		HashMap<String,Map<String,Object>> map = new HashMap<>();
		HashMap<String,Object> dayData = new HashMap<>();
		HashMap<String,Object> hourData = new HashMap<>();
		HashMap<String,Object> totalData = new HashMap<>();
		
		dayData.put("total", list.size());
		dayData.put("rows", list);
		
		hourData.put("total", hourlist.size());
		hourData.put("rows", hourlist);		
		
		totalData.put("total", totallist.size());
		totalData.put("rows",totallist);
		
		map.put("dayData", dayData);
		map.put("hourData", hourData);
		map.put("totalData", totalData);
		
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping("/totalpv")
	public Map<String,Object> totalpv(@RequestParam("address")String address)
	{
		AddrTotalpvBean at = new AddrTotalpvBean();
		at.setAddress(address);

		System.out.println(address);
		
		ArrayList<AddrTotalpvBean> totallist = (ArrayList<AddrTotalpvBean>) atservice.query(at);
		
		HashMap<String,Object> totalData = new HashMap<>();
		
		totalData.put("total", totallist.size());
		totalData.put("rows",totallist);
		
		return totalData;
	}
	
	
	@ResponseBody
	@RequestMapping("/iponline")
	public Map<String,Object> iponline(@RequestParam("ip")String ip)
	{
		IpOnlineBean ipon = new IpOnlineBean();
		ipon.setIp(ip);
		
		int[] flag18 = {0,0,0,0};
		int[] flag19 = {0,0,0,0};
		
		
		IpOnlineDayDto iod18 = new IpOnlineDayDto();
		iod18.setIpon(ipon);
		iod18.setDay("18");
		List<IpOnlineBean> list18 = iposervice.queryByDay(iod18);
		for (IpOnlineBean ipOnlineBean : list18) {
			if("凌晨".equals(ipOnlineBean.getOnlinetime()))
			{
				flag18[0] = 1;
			}
			if("上午".equals(ipOnlineBean.getOnlinetime()))
			{
				flag18[1] = 1;
			}
			if("下午".equals(ipOnlineBean.getOnlinetime()))
			{
				flag18[2] = 1;
			}
			if("晚上".equals(ipOnlineBean.getOnlinetime()))
			{
				flag18[3] = 1;
			}
		}
		
		
		IpOnlineDayDto iod19 = new IpOnlineDayDto();
		iod19.setIpon(ipon);
		iod19.setDay("19");
		List<IpOnlineBean> list19 = iposervice.queryByDay(iod19);
		for (IpOnlineBean ipOnlineBean : list19) {
			if("凌晨".equals(ipOnlineBean.getOnlinetime()))
			{
				flag19[0] = 1;
			}
			if("上午".equals(ipOnlineBean.getOnlinetime()))
			{
				flag19[1] = 1;
			}
			if("下午".equals(ipOnlineBean.getOnlinetime()))
			{
				flag19[2] = 1;
			}
			if("晚上".equals(ipOnlineBean.getOnlinetime()))
			{
				flag19[3] = 1;
			}
		}
		
		
		ArrayList<HashMap<String,Object>> l1 = new ArrayList<>();
		ArrayList<HashMap<String,Object>> l2 = new ArrayList<>();
		
		
		for (IpOnlineBean ipOnlineBean : list18) {
			HashMap<String,Object> m1 = new HashMap<>();
			m1.put("name", ipOnlineBean.getOnlinetime());
			m1.put("value", ipOnlineBean.getOnlinenumber());
			l1.add(m1);
		}
		for(int i = 0; i < flag18.length; i++)
		{
			if(i == 0 && flag18[i] == 0)
			{
				HashMap<String,Object> m1 = new HashMap<>();
				m1.put("name", "凌晨");
				m1.put("value", 0);
				l1.add(m1);
			}
			if(i == 1 && flag18[i] == 0)
			{
				HashMap<String,Object> m1 = new HashMap<>();
				m1.put("name", "上午");
				m1.put("value", 0);
				l1.add(m1);
			}
			if(i == 2 && flag18[i] == 0)
			{
				HashMap<String,Object> m1 = new HashMap<>();
				m1.put("name", "下午");
				m1.put("value", 0);
				l1.add(m1);
			}
			if(i == 3 && flag18[i] == 0)
			{
				HashMap<String,Object> m1 = new HashMap<>();
				m1.put("name", "晚上");
				m1.put("value", 0);
				l1.add(m1);
			}
		}
		
		System.out.println(l1);
		
		
		for (IpOnlineBean ipOnlineBean : list19) {
			HashMap<String,Object> m2 = new HashMap<>();
			m2.put("name", ipOnlineBean.getOnlinetime());
			m2.put("value", ipOnlineBean.getOnlinenumber());
			l2.add(m2);
		}
		for(int i = 0; i < flag19.length; i++)
		{
			if(i == 0 && flag19[i] == 0)
			{
				HashMap<String,Object> m2 = new HashMap<>();
				m2.put("name", "凌晨");
				m2.put("value", 0);
				l2.add(m2);
			}
			if(i == 1 && flag19[i] == 0)
			{
				HashMap<String,Object> m2 = new HashMap<>();
				m2.put("name", "上午");
				m2.put("value", 0);
				l2.add(m2);
			}
			if(i == 2 && flag19[i] == 0)
			{
				HashMap<String,Object> m2 = new HashMap<>();
				m2.put("name", "下午");
				m2.put("value", 0);
				l2.add(m2);
			}
			if(i == 3 && flag19[i] == 0)
			{
				HashMap<String,Object> m2 = new HashMap<>();
				m2.put("name", "晚上");
				m2.put("value", 0);
				l2.add(m2);
			}
		}
		
		System.out.println(l2);
		
		HashMap<String,Object> map = new HashMap<>();
		map.put("eighteen", l1);
		map.put("nineteen", l2);
		
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping("/ipAddr")
	public Map<String,Object> addr(@RequestParam("ip")String ip)
	{
		HashMap<String,Object> map = new HashMap<>();
		
		IpAddrBean ia = new IpAddrBean();
		ia.setIp(ip);
		
		List<IpAddrBean> list = ipadservice.queryByIp(ia);
		
		ArrayList<String> xAxis = new ArrayList<>();
		ArrayList<Long> series = new ArrayList<>();
		
		for (IpAddrBean lpAddr : list) {
			xAxis.add(lpAddr.getAddress());
			series.add(lpAddr.getOnlineaddress());
		}
		
		map.put("xAxis", xAxis);
		map.put("series", series);
		
		return map;
	}

	
	@ResponseBody
	@RequestMapping("/hourOnline")
	public Map<String,Object> hourOnline()
	{
		List<HourOnlineBean> hourlist = hoservice.query(null);
		
		List<Date> timeList = new ArrayList<>();
		List<Long> numList = new ArrayList<>();
		
		for (HourOnlineBean hourOnlineBean : hourlist) {
			timeList.add(hourOnlineBean.getTime());
			numList.add(hourOnlineBean.getOnlinenumber());
		}
		
		HashMap<String,Object> map = new HashMap<>();
		map.put("xAxis", timeList);
		map.put("series", numList);
		
		return map;
	}
}


